﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JobOffersBrowser.Helpers;
using JobOffersBrowser.Commands;
using System.Windows;

namespace JobOffersBrowser.ViewModels
{
    public class AdvancedSearchViewModel : ViewModelBase
    {
        private string id;
        private string jobTitle;
        private string email;
        private string phone;
        private List<IndustryViewModel> industries = new List<IndustryViewModel>(ViewModelsHelper.GetAllIndustries());
        private List<CompanyViewModel> companies = new List<CompanyViewModel>(ViewModelsHelper.GetAllCompanies());
        private List<StatusViewModel> statuses = new List<StatusViewModel>(ViewModelsHelper.GetAllStatuses());
        private DateTime? publishDate;
        private List<CountryViewModel> countries = new List<CountryViewModel>(ViewModelsHelper.GetAllCountries());
        private List<CityViewModel> cities;
        private CountryViewModel currentCountry;
        private CityViewModel currentCity;
        private IndustryViewModel currentIndustry;
        private CompanyViewModel currentCompany;
        private StatusViewModel currentStatus;
        private List<OfferViewModel> offers;
        private SearchCommand searchCommand;

        public AdvancedSearchViewModel()
        {
            this.searchCommand = new SearchCommand(this);
        }

        public SearchCommand SearchCommand
        {
            get
            {
                return searchCommand;
            }
            set
            {
                searchCommand = value;
            }
        }

        public IndustryViewModel CurrentIndustry
        {
            get
            {
                return currentIndustry;
            }
            set
            {
                currentIndustry = value;
                OnPropertyChanged("CurrentIndustry");
            }
        }

        public CompanyViewModel CurrentCompany
        {
            get
            {
                return currentCompany;
            }
            set
            {
                currentCompany = value;
                OnPropertyChanged("CurrentCompany");
            }
        }

        public StatusViewModel CurrentStatus
        {
            get
            {
                return currentStatus;
            }
            set
            {
                currentStatus = value;
                OnPropertyChanged("CurrentStatus");
            }
        }

        public string Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
                OnPropertyChanged("Id");
            }
        }

        public string JobTitle
        {
            get
            {
                return jobTitle;
            }
            set
            {
                jobTitle = value;
                OnPropertyChanged("JobTitle");
            }
        }

        public string Email
        {
            get
            {
                return email;
            }
            set
            {
                email = value;
                OnPropertyChanged("Email");
            }
        }

        public string Phone
        {
            get
            {
                return phone;
            }
            set
            {
                phone = value;

                OnPropertyChanged("Phone");
            }
        }

        public List<IndustryViewModel> Industries
        {
            get
            {
                return industries;
            }
            set
            {
                industries = value;
                OnPropertyChanged("Industries");
            }
        }

        public List<CompanyViewModel> Companies
        {
            get
            {
                return companies;
            }
            set
            {
                companies = value;
                OnPropertyChanged("Companies");
            }
        }

        public List<StatusViewModel> Statuses
        {
            get
            {
                return statuses;
            }
            set
            {
                statuses = value;
                OnPropertyChanged("Statuses");
            }
        }

        public DateTime? PublishDate
        {
            get
            {
                return publishDate;
            }
            set
            {
                publishDate = value;
                OnPropertyChanged("PublishDate");
            }
        }

        public List<CountryViewModel> Countries
        {
            get
            {
                return countries;
            }
            set
            {
                countries = value;
                OnPropertyChanged("Country");
            }
        }

        public List<CityViewModel> Cities
        {
            get
            {
                return cities;
            }
            set
            {
                cities = value;
                OnPropertyChanged("Cities");
            }
        }

        public CountryViewModel CurrentCountry
        {
            get
            {
                return currentCountry;
            }
            set
            {
                currentCountry = value;
                Cities = new List<CityViewModel>(ViewModelsHelper.GetCitiesByCountry(CurrentCountry));
                OnPropertyChanged("CurrentCountry");
            }
        }

        public CityViewModel CurrentCity
        {
            get
            {
                return currentCity;
            }
            set
            {
                currentCity = value;
                OnPropertyChanged("CurrentCity");
            }
        }

        public List<OfferViewModel> Offers
        {
            get
            {
                return offers;
            }
            set
            {
                offers = value;
                OnPropertyChanged("Offers");
            }
        }

        internal void Search()
        {
            Offers = new List<OfferViewModel>(ViewModelsHelper.GetAllOffers());

            FilterByID();

            FilterByJobTitle();

            FilterByEmail();

            FilterByPhone();

            FilterByDate();

            FilterByIndustry();

            FilterByCompany();

            FilterByStatus();

            FilterByCountry();

            FilterByCity();
        }
  
        private void FilterByCity()
        {
            if (CurrentCity != null)
            {
                Offers = Offers.Where(o => o.City == CurrentCity.CityName).ToList();
            }
        }
  
        private void FilterByCountry()
        {
            if (CurrentCountry != null)
            {
                Offers = Offers.Where(o => o.Country == CurrentCountry.CountryName).ToList();
            }
        }
  
        private void FilterByStatus()
        {
            if (this.CurrentStatus != null)
            {
                Offers = Offers.Where(o => o.Status == CurrentStatus.StatusName).ToList();
            }
        }
  
        private void FilterByCompany()
        {
            if (currentCompany != null)
            {
                Offers = Offers.Where(o => o.Company.Name == CurrentCompany.Name).ToList();
            }
        }
  
        private void FilterByIndustry()
        {
            if (CurrentIndustry != null)
            {
                Offers = Offers.Where(o => o.Industry.IndustryName == CurrentIndustry.IndustryName).ToList();
            }
        }
  
        private void FilterByDate()
        {
            if (PublishDate != null)
            {
                Offers = Offers
                               .Where(
                    o => o.PublishDate.Day == PublishDate.Value.Day &&
                         o.PublishDate.Year == PublishDate.Value.Year &&
                         o.PublishDate.Month == PublishDate.Value.Month)
                               .ToList();
            }
        }

        private void FilterByPhone()
        {
            if (Phone != null)
            {
                Offers = Offers.Where(o => o.Company.Phone == Phone).ToList();
            }
        }

        private void FilterByEmail()
        {
            if (Email != null)
            {
                Offers = Offers.Where(o => o.Company.Email == Email).ToList();
            }
        }

        private void FilterByJobTitle()
        {
            if (JobTitle != null)
            {
                Offers = Offers.Where(o => o.JobTitle.ToLower().Contains(JobTitle.ToLower())).ToList();
            }
        }

        private void FilterByID()
        {
            if (!String.IsNullOrEmpty(Id))
            {
                int id = 0;
                try
                {
                    id = int.Parse(Id);
                }
                catch
                {
                    MessageBox.Show("Id in incorrect form");
                }

                Offers = Offers.Where(o => o.OfferID == id).ToList();
            }
        }
    }
}